package main

import (
	"strings"
	"fmt"
)

//最长公共前缀
func LongestCommonPrefix(strs []string) string {
	if len(strs)==0{
		return ""
	}
	minlen:=0
	minstr:=""
	for i,v:=range strs{
		if v==""{
			return ""
		}
		if i==0 || len(v)<minlen{
			minlen = len(v)
			minstr = v
		}

	}
	find:=""
	for i:=0;i<minlen;i++{
		//fmt.Printf("%d:\t%s\n",i,minstr[i:i+1])
		flag:=1
		for _,v:=range strs{
			if v==minstr{
				continue
			}
			//fmt.Printf("%s:\t%s\n",minstr[i:i+1],v[i:i+1])
			if minstr[i:i+1]!=v[i:i+1]{
				flag=0
				break
			}
		}
		if flag==0{
			break
		}else {
			find = find + minstr[i:i+1]
		}
	}

	//fmt.Println(minlen,minstr,find)
	return find
}


//最长公共前缀
func LongestCommonPrefix2(strs []string) string {
	if len(strs)==0{
		return ""
	}

	find := strs[0]
	if find==""{
		return ""
	}
	for i:=1;i<len(strs);i++{
		for !strings.HasPrefix(strs[i],find){
			find = find[0:len(find)-1]
			if find==""{
				return ""
			}
		}
	}

	fmt.Println(find)

	return find
}
